/*
 * CODE WAS TAKEN FROM HERE 
 * http://www.go4expert.com/forums/showthread.php?t=15667
 *  
 */
package CalendarComponents;

import java.util.Calendar;

import javax.swing.Action;
import javax.swing.JMenu;

@SuppressWarnings("serial")
public class Clock extends JMenu implements Runnable
{
	Thread runner;
	
	public Clock() {
		this.start();
	}

	public Clock(String s) {
		super(s);
		this.start();
	}

	public Clock(Action a) {
		super(a);
		this.start();
	}

	public Clock(String s, boolean b) {
		super(s, b);
		this.start();
	}

	@Override
	public void run()
	{
		while (runner == Thread.currentThread() )
	       {
	        	this.setText(timeNow());
	                                                         //define thread task
	           try
	             {
	               Thread.sleep(1000);
	             }
	              catch(InterruptedException e)
	                  {
	                    System.out.println("Thread failed");
	                  }
	                  
	       }
		
	}
	
    public void start()
    {
      if(runner == null) runner = new Thread(this);
      runner.start();
                                                            //method to start thread
    }
    
    public String timeNow()
    {
      Calendar now = Calendar.getInstance();
      int hrs = now.get(Calendar.HOUR_OF_DAY);
      int min = now.get(Calendar.MINUTE);
      int sec = now.get(Calendar.SECOND);
      
      String time = zero(hrs)+":"+zero(min)+":"+zero(sec);
      
      return time;
    }
    
    public String zero(int num)
    {
      String number=( num < 10) ? ("0"+num) : (""+num);
      return number;                                    //Add leading zero if needed
      
    }

}
